import requests
import os
import sys
from lxml import etree

cwd = os.getcwd()
sys.path.append(cwd)
from pub_func import DownloadFile, folder_exist

def send_requests(url, headers):
    response = requests.get(url=url, headers=headers)
    html = etree.HTML(response.text)
    result_list = html.xpath('/html/body/pre/a/@href')
    return result_list

base_url = 'http://192.168.70.41:5234/pro'
headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
}

# 得到所有时间
time_list = send_requests(base_url, headers)
# 遍历所有的时间 第一个元素为../
for time_str in time_list[1:]:
    time_url = os.path.join(base_url, time_str)
    username_list = send_requests(time_url, headers)
    # 遍历所有的用户 第一个元素为../
    for username in username_list[1:]:
        username_url = os.path.join(time_url, username)
        wav_list = send_requests(username_url, headers)
        # 遍历所有的音频文件 第一个元素为../
        for wav in wav_list[1:]:
            wav_url = os.path.join(username_url, wav)
            # response = requests.get(url=wav_url, headers=headers)
            print(wav_url)
            dir_name = os.path.join('./70', time_str)
            folder_exist(dir_name)
            DownloadFile(wav_url, dir_name, wav)

            
